let DBConfig = require("../config/DBConfig_lb");

module.exports = {
  async selectUserInfo(req, resp) {
    let { userName } = req.body;
    let gen = "女";
    let name = "张";
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db(
        "select * from register where gender=? and nickname like ?",
        [gen, `${name}%`],
        (err, data) => {
          if (!err) {
            console.log(data);
            resolve(data);
          } else {
            reject(err);
          }
        }
      );
    });
    console.log(userInfo);
    resp.send(userInfo);
  },
  async standardAdd(req, resp) {
    let {
      stan_id,
      stan_unild,
      stan_name,
      stan_standardUnit,
      stan_upper,
      stan_lower,
      stan_normalMsg,
      stan_upMsg,
      stan_lowerMsg,
    } = req.body;
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db(
        "insert into standard(stan_id,stan_unild,stan_name,stan_standardUnit,stan_upper,stan_lower,stan_normalMsg,stan_upMsg,stan_lowerMsg,stan_contrast) values (?,?,?,?,?,?,?,?,?,?)",
        [
          stan_id,
          stan_unild,
          stan_name,
          stan_standardUnit,
          stan_upper,
          stan_lower,
          stan_normalMsg,
          stan_upMsg,
          stan_lowerMsg,
          "0",
        ],
        (err, data) => {
          if (!err) {
            console.log(data);
            resolve(data);
          } else {
            console.log(err);
            reject(err);
          }
        }
      );
    });
    console.log(userInfo);
    resp.send(userInfo);
  },
  async standardDel(req, resp) {
    let { stan_id } = req.body;
    let gen = "女";
    let name = "张";
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db(
        "delete from standard where stan_id=?",
        [stan_id],
        (err, data) => {
          if (!err) {
            console.log(data);
            resolve(data);
          } else {
            reject(err);
          }
        }
      );
    });
    console.log(userInfo);
    resp.send(userInfo);
  },
  async standardEdit(req, resp) {
    let {
      stan_unild,
      stan_name,
      stan_standardUnit,
      stan_upper,
      stan_lower,
      stan_normalMsg,
      stan_upMsg,
      stan_lowerMsg,
      stan_id,
    } = req.body;
    let gen = "女";
    let name = "张";
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db(
        "update standard set stan_unild=?,stan_name=?,stan_standardUnit=?,stan_upper=?,stan_lower=?,stan_normalMsg=?,stan_upMsg=?,stan_lowerMsg=?,stan_contrast=" +
          0 +
          " where stan_id=?",
        [
          stan_unild,
          stan_name,
          stan_standardUnit,
          stan_upper,
          stan_lower,
          stan_normalMsg,
          stan_upMsg,
          stan_lowerMsg,
          stan_id,
        ],
        (err, data) => {
          if (!err) {
            console.log(data);
            resolve(data);
          } else {
            reject(err);
          }
        }
      );
    });
    console.log(userInfo);
    resp.send(userInfo);
  },
  async standardFind(req, resp) {
    let { stan_id } = req.query;
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db(
        "select * from standard where stan_id=?",
        [stan_id],
        (err, data) => {
          if (!err) {
            console.log(data);
            resolve(data);
          } else {
            reject(err);
          }
        }
      );
    });
    console.log(userInfo);
    resp.send(userInfo);
  },
  async standardSearch(req, resp) {
    let { stan_name } = req.query;
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db(
        "select * from standard where stan_name=?",
        [stan_name],
        (err, data) => {
          if (!err) {
            console.log(data);
            resolve(data);
          } else {
            reject(err);
          }
        }
      );
    });
    console.log(userInfo);
    resp.send(userInfo);
  },
  async organizationAll(req, resp) {
    let { stan_name } = req.query;
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db("select * from organization", [], (err, data) => {
        if (!err) {
          console.log(data);
          resolve(data);
        } else {
          reject(err);
        }
      });
    });
    console.log(userInfo);
    resp.send(userInfo);
  },
  async organizationFindById(req, resp) {
    let { org_id } = req.query;
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db(
        "select * from organization where org_id=?",
        [org_id],
        (err, data) => {
          if (!err) {
            console.log(data);
            resolve(data);
          } else {
            reject(err);
          }
        }
      );
    });
    console.log(userInfo);
    resp.send(userInfo);
  },
  async set_mealAll(req, resp) {
    let { stan_name } = req.query;
    let userInfo = await new Promise((resolve, reject) => {
      DBConfig.db("select * from set_meal", [], (err, data) => {
        if (!err) {
          console.log(data);
          resolve(data);
        } else {
          reject(err);
        }
      });
    });
    console.log(userInfo);
    resp.send(userInfo);
  },
};
